/*
 * BuddyLoader.java
 *
 * Created on February 19, 2008, 7:52 PM
 */

package StudyBuddy;

import java.io.*;
import java.net.*;
import javax.swing.*;
/**
 *
 * @author  Gabriel
 */
public class BuddyLoader extends javax.swing.JFrame implements Runnable{
    

    PluginCollection collection;
    
    /** Creates new form BuddyLoader */
    public BuddyLoader() {
        collection = new PluginCollection();
        initComponents();
    }
    
    public void run(){
            
       this.setVisible(true);
        
    }
    
    public void refresh(){
        
        BuddyCollection.removeAllItems();
        for (int i = 0; i<collection.getSize(); i++){ 
            System.out.println(collection.getPluginName(i));
            BuddyCollection.addItem(collection.getPluginName(i));
        }
    
    }
    
    
    public PluginCollection getPluginCollection(){
 //       System.out.println("Collection.toString " + collection.toString());
        return collection;       
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        StartBuddy = new javax.swing.JButton();
        AddBuddy = new javax.swing.JButton();
        RndBuddy = new javax.swing.JButton();
        DeleteBuddy = new javax.swing.JButton();
        BuddyCollection = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        Statisics = new javax.swing.JButton();
        ChangePW = new javax.swing.JButton();
        Logout = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Select Buddy", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18)));

        StartBuddy.setFont(new java.awt.Font("Tahoma", 1, 14));
        StartBuddy.setText("Start Buddy");
        StartBuddy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartBuddyActionPerformed(evt);
            }
        });

        AddBuddy.setFont(new java.awt.Font("Tahoma", 1, 14));
        AddBuddy.setText("Add Buddy");
        AddBuddy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddBuddyActionPerformed(evt);
            }
        });

        RndBuddy.setFont(new java.awt.Font("Tahoma", 1, 14));
        RndBuddy.setText("Random Buddy");
        RndBuddy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RndBuddyActionPerformed(evt);
            }
        });

        DeleteBuddy.setFont(new java.awt.Font("Tahoma", 1, 14));
        DeleteBuddy.setText("Delete Buddy");
        DeleteBuddy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteBuddyActionPerformed(evt);
            }
        });

        BuddyCollection.setFont(new java.awt.Font("Tahoma", 1, 14));

        jButton1.setLabel("write");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setLabel("load");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(RndBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 363, Short.MAX_VALUE)
                        .addComponent(StartBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(BuddyCollection, 0, 610, Short.MAX_VALUE)
                            .addComponent(AddBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(DeleteBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(523, Short.MAX_VALUE))))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(BuddyCollection, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(AddBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(DeleteBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 217, Short.MAX_VALUE)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(RndBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(StartBuddy, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        Statisics.setFont(new java.awt.Font("Tahoma", 1, 14));
        Statisics.setText("Statisics");
        Statisics.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StatisicsActionPerformed(evt);
            }
        });

        ChangePW.setFont(new java.awt.Font("Tahoma", 1, 14));
        ChangePW.setText("Change Password");
        ChangePW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangePWActionPerformed(evt);
            }
        });

        Logout.setFont(new java.awt.Font("Tahoma", 1, 14));
        Logout.setText("Logout");
        Logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogoutActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Logout, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ChangePW, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 214, Short.MAX_VALUE)
                        .addComponent(Statisics, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Logout, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ChangePW, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Statisics, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        collection.writeListToFile();
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
            try{
            collection.loadListFromFile(); 
            }

            catch (MalformedURLException e) {
                    JOptionPane.showMessageDialog(null,"The URL is Malformed. But since it's generated by toURL you should never see this Error");

            } catch (ClassNotFoundException e) {
                    JOptionPane.showMessageDialog(null,"ClassNotFoundException \nThis means quite simply that the \"class\" was not found.\nThe file you're trying to open is NOT A CLASS!");

            }catch (IOException e) {
                    JOptionPane.showMessageDialog(null,"General IO Exception - Figure the problem out yourself");

            } catch (InstantiationException e) {
                    JOptionPane.showMessageDialog(null,"Instantiation Exception - Class may not be a class");

            }catch (IllegalAccessException e) {
                    JOptionPane.showMessageDialog(null,"Illegal Access Exception - Class may be in use");

            }catch (NoClassDefFoundError e){
                    JOptionPane.showMessageDialog(null,"No Class Def Found \nEither dependent classes are missing or your buddy is not in a folder of name equal to the package");
            }
            catch (Exception e){
                    JOptionPane.showMessageDialog(null,"Strange Exception ... We couldn't figure out what it was!! YOU'RE ON YOUR OWN!!");
            }
            refresh();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void StartBuddyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartBuddyActionPerformed
        collection.runPlugin((int)BuddyCollection.getSelectedIndex());
        //DifficultyFrame DF = new DifficultyFrame(this);
        //DF.setVisible(true);
    }//GEN-LAST:event_StartBuddyActionPerformed

    private void AddBuddyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddBuddyActionPerformed
        JFileChooser sc = new JFileChooser();
        int returnValue = sc.showOpenDialog(null);
        try{
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = sc.getSelectedFile();
                collection.addPlugin(selectedFile);
                BuddyCollection.addItem(collection.getPluginName(collection.getSize()-1));
                }
            }
        
        catch (MalformedURLException e) {
                JOptionPane.showMessageDialog(null,"The URL is Malformed. But since it's generated by toURL you should never see this Error");

        } catch (ClassNotFoundException e) {
                JOptionPane.showMessageDialog(null,"ClassNotFoundException \nThis means quite simply that the \"class\" was not found.\nThe file you're trying to open is NOT A CLASS!");

        }catch (IOException e) {
                JOptionPane.showMessageDialog(null,"General IO Exception - Figure the problem out yourself");

        } catch (InstantiationException e) {
                JOptionPane.showMessageDialog(null,"Instantiation Exception - Class may not be a class");

        }catch (IllegalAccessException e) {
                JOptionPane.showMessageDialog(null,"Illegal Access Exception - Class may be in use");

        } catch (Exception e){
            JOptionPane.showMessageDialog(null,"Strange Exception");
            
        }

            // refresh();
    }//GEN-LAST:event_AddBuddyActionPerformed

    private void RndBuddyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RndBuddyActionPerformed
        this.setVisible(false);
        dispose();
    }//GEN-LAST:event_RndBuddyActionPerformed

    private void DeleteBuddyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteBuddyActionPerformed
        collection.removePlugin(BuddyCollection.getSelectedIndex());
        BuddyCollection.removeAllItems();
        for (int i = 0; i<collection.getSize(); i++){
            System.out.println(i);
            BuddyCollection.addItem(collection.getPluginName(i));
        }

    }//GEN-LAST:event_DeleteBuddyActionPerformed

    private void StatisicsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StatisicsActionPerformed
        StatisticsGUI newStatsFrame = new StatisticsGUI(this.getPluginCollection());
        newStatsFrame.run();
    }//GEN-LAST:event_StatisicsActionPerformed

    private void ChangePWActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangePWActionPerformed
        ChangePasswordFrame CPF = new ChangePasswordFrame();
        CPF.setVisible(true);
    }//GEN-LAST:event_ChangePWActionPerformed

    private void LogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogoutActionPerformed
        LoginFrame LF = new LoginFrame();
        LF.setVisible(true);
        this.setVisible(false);
        dispose();
    }//GEN-LAST:event_LogoutActionPerformed
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddBuddy;
    private javax.swing.JComboBox BuddyCollection;
    private javax.swing.JButton ChangePW;
    private javax.swing.JButton DeleteBuddy;
    private javax.swing.JButton Logout;
    private javax.swing.JButton RndBuddy;
    private javax.swing.JButton StartBuddy;
    private javax.swing.JButton Statisics;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel5;
    // End of variables declaration//GEN-END:variables
    
}
